﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Data_Access_Layer;
using RealtorsPortal.Entities;
using RealtorsPortal.Business_Logic_Layer;
using System.Data;




namespace RealtorsPortal.AdminCP
{
    public partial class ManageCategories : System.Web.UI.Page
    {


        CategoriesDAL cDAL = new CategoriesDAL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCategoryList();
            }
        }

        protected void LoadCategoryList()
        {
            CategoriesDAL cateDAL = new CategoriesDAL();
            grdCategoriesList.DataSource = cateDAL.GetCategoriessData();
            grdCategoriesList.DataBind();
        }

        protected void grdCategoriesList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {


            grdCategoriesList.EditIndex = -1;
            LoadCategoryList();
        }

        protected void grdCategoriesList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdCategoriesList.EditIndex = e.NewEditIndex;
            LoadCategoryList();
        }

        protected void grdCategoriesList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdCategoriesList.EditIndex = -1;
            LoadCategoryList();
        }

        protected void grdCategoriesList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdCategoriesList.EditIndex = e.NewPageIndex;
            LoadCategoryList();
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grdCategoriesList.PageSize = int.Parse(ddlPageSize.Text);
            this.LoadCategoryList();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Category cate = new Category();
            if (addCate(cate))
            {
                lblStatus.Text = "Add successed!";
                
            }
            else
            {
                lblStatus.Text = "Not successed, check your typed information!";
            }


            LoadCategoryList();
        }

        /// <summary>
        /// Update category method
        /// </summary>
        /// <param name="index">The index of the category to update</param>
        /// <returns></returns>
        protected bool UpdateCate(int RowIndex)
        {
            //initial category object
            Category c = new Category();
            c.CategoryId = int.Parse(grdCategoriesList.DataKeys[RowIndex].Value.ToString());
            c.CategoryName = ((TextBox)grdCategoriesList.Rows[RowIndex].FindControl("txtCateName")).Text;
            c.Description = ((TextBox)grdCategoriesList.Rows[RowIndex].FindControl("txtDescription")).Text;
            c.IsActive = ((CheckBox)grdCategoriesList.Rows[RowIndex].FindControl("chkIsActive_edit")).Checked;

            //perform update, with the object and grid row index           
            if (cDAL.UpdateCategory(c))
            {
                return true;

            }
            else
            {

                return false;
            }
        }

        /// <summary>
        /// Delete category method  
        /// </summary>
        /// <param name="index">the index of category in the database</param>
        /// <returns></returns>
        protected bool DeleteCate(int index)
        {
            if (cDAL.DeleteCategory(index))
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        protected void grdCategoriesList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
                if(e.CommandName !=null)
                {
                    int index = Convert.ToInt32(e.CommandArgument);
                    switch (e.CommandName)
                    {
                        case "UpdateCommand":
                            UpdateCate(index);
                            grdCategoriesList.EditIndex = -1;
                            LoadCategoryList();
                            break;
                        case "DeleteCommand":
                            int cateIndex = int.Parse(grdCategoriesList.DataKeys[index].Value.ToString());
                            DeleteCate(cateIndex);
                            grdCategoriesList.EditIndex = -1;
                            LoadCategoryList();
                            break;
                    }
                }
        }


        protected bool addCate(Category c)
        {
            c.CategoryName = txtCategoryName.Text;
            c.Description = txtDescription.Text;
            c.IsActive = chkActived.Checked;

            if (cDAL.InsertCategory(c))
            {
                
                return true;
            }
            else
            {
                
                return false;
            }
        }
    }
}